﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Category;
using DataObject.Product;

public partial class Ajax_Product_Ajax_PriceFilter : System.Web.UI.Page
{
    protected string strTagA = "<a href=\"{0}\" title=\"{1}\">{1}</a>";
    protected int CategoryId;
    protected int PageIndex;
    protected int MinPrice;
    protected int MaxPrice;
    protected int TotalRecords;

    protected string CategoryName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        CategoryId = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
        PageIndex = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
        MinPrice = Utils.GetIdFromRequestString(Utils.MinPrice, SettingValues.PageIndex);
        MaxPrice = Utils.GetIdFromRequestString(Utils.MaxPrice, SettingValues.PageIndex);

        if (!IsPostBack)
            ListProductByCategory();
    }

    private void ListProductByCategory()
    {
        if (CategoryId > 0)
        {
            var lstProduct = ProductController.Product_ListPaginationByCategory(string.Empty, CategoryId, 0, 0, PageIndex, SettingValues.PageSize_12, ref TotalRecords);

            if (lstProduct != null && lstProduct.Rows.Count > 0)
            {
                CategoryName = CategoryController.GetCategoryName(CategoryId);

                rptProduct.DataSource = lstProduct;
                rptProduct.DataBind();
                var cat = UrlHelpers.ListProductPaginationByCategory(CategoryId, CategoryName, "{0}");

                ltPager.Text = PagerUtility.PageLinks(PageIndex, SettingValues.PageSize_32, TotalRecords, SettingValues.PageStep,
                                                        cat);
                Page.Title = string.Format(".: {0} {1} :.", CategoryName, Constants.SITE_TITLE);
            }
        }
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var categoryId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
            var productId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfProductId")).Value);
            var title = ((HiddenField)e.Item.FindControl("hfTitle")).Value;

            var ltrTitle = ((Literal)e.Item.FindControl("ltrTitle"));
            var ltrShoppingCart = ((Literal)e.Item.FindControl("ltrShoppingCart"));
            var ltrImage = ((Literal)e.Item.FindControl("ltrImage"));

            var imgHref = "/images/products/home_3/4.jpg";// string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=190&Fix=highlight", Constants.ThumbImage, productId, (int)CategoryType.Product);


            var categoryName = CategoryController.GetCategoryName(categoryId);
            var pHref = UrlHelpers.ProductDetail(categoryName, ConvertUtility.ToInt32(productId), title);
            ltrTitle.Text = string.Format(strTagA, pHref, title);

            ltrShoppingCart.Text = "<a href=\"javascript:void(0);\" class=\"add-to-cart pull-left\" onclick=\"addToCart('" + productId + "')\"><i class=\"fa fa-shopping-cart\"></i></a><a class=\"beta-btn primary\" href=\"" + pHref + "\">Chi tiết <i class=\"fa fa-chevron-right\"></i></a>";

            ltrImage.Text = string.Format("<a href=\"{0}\" title=\"{1}\" class=\"thumb\"><img src=\"{2}\" alt=\"\" /></a>", pHref, title, imgHref);
        }
    }
}